package com.nj.dms.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nj.dms.bean.User;
import com.nj.dms.dao.UserDao;
import com.nj.dms.dao.impl.UserDaoImpl;

/**
 * 查找一个用户
 */
@WebServlet("/FindUserByUserName")
public class FindUserByUserName extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 获取一个用户的ID

		String userName = request.getParameter("userName");

		UserDao userDao = new UserDaoImpl();

		User user = userDao.findUser(userName);

		if (user != null) {

			System.out.println(user);

			// 用户存在，跳转到用户查看页

			request.setAttribute("year", new SimpleDateFormat("yyyy").format(user.getBirthday()));
			request.setAttribute("month", new SimpleDateFormat("MM").format(user.getBirthday()));
			request.setAttribute("day", new SimpleDateFormat("dd").format(user.getBirthday()));
			request.setAttribute("user", user);

			request.getRequestDispatcher("user/look-user.jsp").forward(request, response);

		} else {

			// 用户不存在，跳转到用户列表
			response.sendRedirect("UserList");

		}

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
